<?php
namespace app\api\controller;
use think\Db;
use lib\Curl;
class Invite extends Base{
    public function register(){
        $this->assign('android',$_SERVER['HTTP_HOST'].'/static/download/kabb.apk');
        $this->assign('ios',$_SERVER['HTTP_HOST'].'/static/download/kabb.apk');
    	return $this->fetch();
    }
    /**
    * 发送手机短信
    */
    private function sendSmsByzk($phone, $content){
        if(!empty($phone) && !empty($content)){
            $content.="";
            $url="http://sdk.entinfo.cn:8061/webservice.asmx/mdsmssend?sn=SDK-FLH-010-00309&pwd=".strtoupper(md5('SDK-FLH-010-003092fEad6-f'))."&mobile=".$phone."&content=".$content."&ext=&stime=&rrid=&msgfmt=";
            $xml=Curl::http_curl($url);
            $xmlob=simplexml_load_string($xml);
            $arr=(array)$xmlob;
            
            if($arr[0] > 0){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
        
    }
    //发送短信接口
    public function sendSms(){
        $phone=input("param.phone");
        if(!isset($phone) && empty($phone)){
            return json(['code' => 501, 'msg' => '手机号不能为空']);exit();
        }
        $time=60*12;            //几分钟后过期
        $verifyCode = rand(1000,9999);
        $content = "【卡帮帮】尊敬的用户您好，您的短信验证码为：".$verifyCode.",感谢您的使用，祝您生活愉快！";
        $result = Db::name('sms_log')->insertGetId(['phone' => $phone,'content' => $content,  'time' => time() + $time * 60, 'code' => $verifyCode]);
        if(!$result){
            return json(['code' => 502, 'msg' => '发送失败，稍后重试哟']);exit();
        }else{
            $res = $this->sendSmsByzk($phone,$content);
            if($res){
                return json(['code' => 200, 'msg' => '发送成功','data'=>$verifyCode]);exit();
            }else{
                return json(['code' => 503, 'msg' => '发送失败，稍后重试']);exit();
            }
        }
    }
    //注册
    public function registering(){
        if(!empty(input("param.phone"))){
            $data['phone']=input("param.phone");
        }else{
            return json(['code' => 501, 'msg' => '手机号不能为空']);exit();
        }
        if(!empty(input("param.password"))){
            $data['password']=input("param.password");
        }else{
            return json(['code' => 502, 'msg' => '密码不能为空']);exit();
        }
        $user=Db::name('users')->where(array('phone'=>$data['phone']))->find();
        if (empty($user)) {
            $data['password']=md5('tmd'.$data['password']);
            $data['time']=time();
            $data['status']=1;
            $data['invitephone']=input("param.ytel");
            $id=Db::name('users')->insertGetId($data);
            if($id){
                return json(['code' => 200, 'msg' => '注册成功']);exit();
            }else{
                return json(['code' => 504, 'msg' => '注册失败']);exit();
            }
        }else{
            return json(['code' => 503, 'msg' => '该手机号注册过了']);exit();
        }
    }
}
